package uk.co.droidinactu.heartratemonitor;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import uk.co.droidinactu.common.model.AbstractDataObj;
import android.content.ContentValues;
import android.location.Location;

public class HeartRateRecord extends AbstractDataObj {

	public static final String DATABASE_TABLE_NAME = "HeartRateRecord";

	public static final String FIELD_NAME_DATE_TAKEN = "date_taken";
	public static final String FIELD_NAME_HEART_RATE = "heartrate";
	public static final String FIELD_NAME_LOCATION_ACCURACY = "location_accuracy";
	public static final String FIELD_NAME_LOCATION_ALTITUDE = "location_altitude";
	public static final String FIELD_NAME_LOCATION_BEARING = "location_bearing";
	public static final String FIELD_NAME_LOCATION_LATITUDE = "location_latitude";
	public static final String FIELD_NAME_LOCATION_LONGITUDE = "location_longitude";
	public static final String FIELD_NAME_LOCATION_PROVIDER = "location_provider";
	public static final String FIELD_NAME_LOCATION_SPEED = "location_speed";
	public static final String FIELD_NAME_LOCATION_TIME = "location_time";

	/**
	 * this number denotes the version of this class. so that we can decide if
	 * the structure of this class is the same as the one being deserialized
	 * into it
	 */
	private static final long serialVersionUID = 1L;

	public Date dateTaken;

	public int heartRate;
	public Location location = new Location("dummy");

	public HeartRateRecord() {
	}

	public HeartRateRecord(final HashMap<String, String> aRow) {
		super(aRow);
		dateTaken = new Date(aRow.get(FIELD_NAME_DATE_TAKEN));
		heartRate = Integer.parseInt(aRow.get(FIELD_NAME_HEART_RATE));
		location = new Location(aRow.get(FIELD_NAME_LOCATION_PROVIDER));
		location.setAccuracy(Float.parseFloat(aRow.get(FIELD_NAME_LOCATION_ACCURACY)));
		location.setAltitude(Double.parseDouble(aRow.get(FIELD_NAME_LOCATION_ALTITUDE)));
		location.setBearing(Float.parseFloat(aRow.get(FIELD_NAME_LOCATION_BEARING)));
		location.setLatitude(Double.parseDouble(aRow.get(FIELD_NAME_LOCATION_LATITUDE)));
		location.setLongitude(Double.parseDouble(aRow.get(FIELD_NAME_LOCATION_LONGITUDE)));
		location.setProvider(aRow.get(FIELD_NAME_LOCATION_PROVIDER));
		location.setSpeed(Float.parseFloat(aRow.get(FIELD_NAME_LOCATION_SPEED)));
		location.setTime(Long.parseLong(aRow.get(FIELD_NAME_LOCATION_TIME)));
	}

	@Override
	public ContentValues getContentValues() {
		final ContentValues map = super.getContentValues();
		map.put(FIELD_NAME_DATE_TAKEN, makeSafeSQL(dateToString(dateTaken)));
		map.put(FIELD_NAME_HEART_RATE, heartRate);
		map.put(FIELD_NAME_LOCATION_ACCURACY, location.getAccuracy());
		map.put(FIELD_NAME_LOCATION_ALTITUDE, location.getAltitude());
		map.put(FIELD_NAME_LOCATION_BEARING, location.getBearing());
		map.put(FIELD_NAME_LOCATION_LATITUDE, location.getLatitude());
		map.put(FIELD_NAME_LOCATION_LONGITUDE, location.getLongitude());
		map.put(FIELD_NAME_LOCATION_PROVIDER, location.getProvider());
		map.put(FIELD_NAME_LOCATION_SPEED, location.getSpeed());
		map.put(FIELD_NAME_LOCATION_TIME, location.getTime());
		return map;
	}

	@Override
	public HashMap<Integer, ArrayList<String>> getFields() {
		final HashMap<Integer, ArrayList<String>> fields = super.getFields();

		int x = fields.size();
		fields.put(x++, getArrayList(FIELD_NAME_DATE_TAKEN, "VARCHAR(255) NOT NULL"));
		fields.put(x++, getArrayList(FIELD_NAME_HEART_RATE, "VARCHAR(255) NOT NULL"));
		fields.put(x++, getArrayList(FIELD_NAME_LOCATION_ACCURACY, "VARCHAR(255) NOT NULL"));
		fields.put(x++, getArrayList(FIELD_NAME_LOCATION_ALTITUDE, "VARCHAR(255) NOT NULL"));
		fields.put(x++, getArrayList(FIELD_NAME_LOCATION_BEARING, "VARCHAR(255) NOT NULL"));
		fields.put(x++, getArrayList(FIELD_NAME_LOCATION_LATITUDE, "VARCHAR(255) NOT NULL"));
		fields.put(x++, getArrayList(FIELD_NAME_LOCATION_LONGITUDE, "VARCHAR(255) NOT NULL"));
		fields.put(x++, getArrayList(FIELD_NAME_LOCATION_PROVIDER, "VARCHAR(255) NOT NULL"));
		fields.put(x++, getArrayList(FIELD_NAME_LOCATION_SPEED, "VARCHAR(255) NOT NULL"));
		fields.put(x++, getArrayList(FIELD_NAME_LOCATION_TIME, "VARCHAR(255) NOT NULL"));
		return fields;
	}

	public String getSqlUpdateFromV001() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getSqlUpdateFromV002() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getTableName() {
		return DATABASE_TABLE_NAME;
	}
}
